package com.caigou.comm.base.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import com.aigestudio.wheelpicker.WheelPicker
import com.caigou.comm.R
import com.caigou.comm.base.ext.getScreenWidth
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/11/09
 */
class YearPickDialog constructor(context: Context) : Dialog(context) {

    private val years = mutableListOf<Int>()

    lateinit var tvCancel: TextView
    lateinit var tvConfirm: TextView
    lateinit var wpYear: WheelPicker

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
        initData()
    }

    private fun initView() {
        setContentView(R.layout.comm_year_pick)
        val attr = window?.attributes as WindowManager.LayoutParams
        //设置dialog在布局中的位置
        attr.gravity = Gravity.BOTTOM
        attr.width = getScreenWidth(context)
        tvCancel = findViewById(R.id.comm_year_pick_dialog_tv_cancel)
        tvConfirm = findViewById(R.id.comm_year_pick_dialog_tv_confirm)
        wpYear = findViewById(R.id.comm_year_pick_dialog_wp_year)

        tvCancel.setOnClickListener {
            dismiss()
        }
    }

    private fun initData() {
        val currentYear = Calendar.getInstance().get(Calendar.YEAR)
        for (year in 2021 until (currentYear + 10)) {
            years.add(year)
        }
        wpYear.data = years
    }

    fun getYear(): Int {
        return years[wpYear.currentItemPosition]
    }
}